/**
  * <br>JAVACC DEMO 1.0<br>
*/
package com.apache.portal.weixin.core.manager.impl.send;

import org.apache.log4j.Logger;

import com.apache.portal.common.util.EnumUtil.ResMsgEnum;
import com.apache.portal.weixin.core.common.BaseService;
import com.apache.portal.weixin.core.common.LoadService;
import com.apache.portal.weixin.core.manager.ResMsgManager;
import com.apache.portal.weixin.entity.BaseVo;
import com.apache.portal.weixin.entity.recv.WxRecvEventMsg;
import com.apache.portal.weixin.entity.recv.WxRecvMsg;
import com.apache.portal.weixin.entity.recv.WxRecvTextMsg;
import com.apache.portal.weixin.entity.send.WxSendMsg;
import com.apache.portal.weixin.entity.send.WxSendNewsMsg;
import com.apache.portal.weixin.entity.send.WxSendTextMsg;

/**
  * description: 文本消息回复接口实现
  * @author Hou DaYu 创建时间：上午09:29:45
  */
public class TextMsgManagerImpl implements ResMsgManager {

	private Logger log = Logger.getLogger(TextMsgManagerImpl.class);

	/* (non-Javadoc)
	 * @see com.using.weixin.wxtools.manager.ResMsgManager#execute(com.using.weixin.wxtools.manager.ResMsgManager.ResMsgEnum, com.using.weixin.wxtools.vo.recv.WxRecvMsg)
	 */
	public WxSendMsg execute(ResMsgEnum resMsg, WxRecvMsg wxRecvMsg) {
		WxSendMsg sendMsg = null;
		try {
			switch (resMsg) {
			case text:
				sendMsg = getWxSendTextMsg(wxRecvMsg);
				break;
			case event:
				sendMsg = getWxSendNewsMsg((WxRecvEventMsg) wxRecvMsg);
				break;
			}
		} catch (Exception e) {
			sendMsg = LoadService.getInstance().getTools().builderSendByRecv(wxRecvMsg);
			sendMsg = new WxSendTextMsg(sendMsg, "请您稍后关注，版本更新中...");
			log.error(e);
		}
		return sendMsg;
	}

	private WxSendMsg getWxSendTextMsg(WxRecvMsg wxRecvMsg) throws Exception {
		BaseService service = LoadService.getInstance().getBeans("service_text");
		WxSendMsg sendMsg = LoadService.getInstance().getTools().builderSendByRecv(wxRecvMsg);
		if (null == service) {
			sendMsg = new WxSendTextMsg(sendMsg, "您发的内容是：" + ((WxRecvTextMsg) wxRecvMsg).getContent());
		} else {
			BaseVo vo = new BaseVo();
			vo.setWxRecvMsg(wxRecvMsg);
			//vo.setParams("", "");
			sendMsg = (WxSendMsg) service.execute(vo);
		}
		return sendMsg;
	}

	private WxSendMsg getWxSendNewsMsg(WxRecvMsg wxRecvMsg) throws Exception {
		WxSendMsg sendMsg = LoadService.getInstance().getTools().builderSendByRecv(wxRecvMsg);
		BaseService service = LoadService.getInstance().getBeans("service_news");
		if (null == service) {
			WxSendNewsMsg news = new WxSendNewsMsg(sendMsg);
			news.addItem("软件服务", "信息系统建设服务", "http://www.demo.com/images/gb/fw_img01.png",
					"http://www.demo.com/article/fw/hxfw/");
			return news;
		} else {
			BaseVo vo = new BaseVo();
			vo.setWxRecvMsg(wxRecvMsg);
			//vo.setParams("", "");
			sendMsg = (WxSendMsg) service.execute(vo);
		}
		return sendMsg;
	}
}
